<?php
class SB_admin extends Admin_Controller {

    function SB_admin() {
        parent::Admin_Controller();
        $this->load->model('SB_admin_m');
        $this->load->library('upload');
        $this->_data['listsidebar'] = 'admin/modules/contenu';
        $this->_data['erreur'] = '';
    }

    function index() {
        is_admin();
        $this->list_admin();
    }
    function login(){
        $this->form_validation->set_rules('ident', 'Username', 'required');
        $this->form_validation->set_rules('pass', 'Password', 'required');
        if($this->input->post('submit')){
        //thuc hien validate form va check csdl
        if ($this->form_validation->run()){
            $name = $this->input->post('ident');
            $pass = $this->input->post('pass');
            $data=array(
                'ident'=>$name,
                'pass'=>md5($pass)
                );
            $query=$this->SB_admin_m->login($data);
            if ($query->num_rows()>0){
                $admin_data=$query->row();
                $this->session->set_userdata('adminid',$admin_data->id);
                $this->session->set_userdata('adminname',$admin_data->nom.' '.$admin_data->prenom);
                $this->session->set_userdata('level',$admin_data->level);
                redirect('SB_admin');
                } else{
                    $this->_data['error'] = 'Nom d\'utilisateur incorrect ou mot de passe';
                    $this->_data['page_title'] = "Conexion";
                    $this->display_view('admin/login');
                    }
                }
            else {
                    $this->_data['page_title'] = "Conexion";
                    $this->display_view('admin/login');
                }
            }
        else {
                //hien thi trang admin
                $this->_data['page_title'] = "Conexion";
                $this->display_view('admin/login');
            }

        }
    
    function list_admin() {
        is_admin();
        $per_page = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($per_page == "") {
            $per_page = 10;
        }
        if ($offset == "") {
            $offset = 0;
        }
        $config['base_url'] = base_url() . 'index.php/SB_admin/list_admin/' . $per_page . '/';
        $config['per_page'] = '10';
        $config['uri_segment'] = 4;
        $config['total_rows'] = $this->db->count_all('admin');
        $this->_data['query'] = $this->SB_admin_m->get_admin($config['per_page'], $offset);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $this->db->count_all('admin');
        $this->_data['page_title'] = "Liste Admin";
        $this->display_view('admin/admin');   
    }

    function add_admin(){
           is_admin();
           $config = array(
            array(
            'field'   => 'ident',
            'label'   => 'ident',
            'rules'   => 'required|xss_clean'
            ),
            array(
            'field'   => 'pass',
            'label'   => 'Passsssssss',
            'rules'   => 'required|xss_clean|matches[confirm_pass]'
            ),
            array(
            'field'   => 'confirm_pass',
            'label'   => 'Confirmation Password',
            'rules'   => 'required|xss_clean'
            ),
            array(
            'field'   => 'nom',
            'label'   => 'nommmmmm',
            'rules'   => 'required|xss_clean'
            ),
            array(
            'field'   => 'prenom',
            'label'   => 'Prenom',
            'rules'   => 'required|xss_clean'
            )
            );
            $this->form_validation->set_rules($config); 
            if($this->input->post('submit')){
                if($this->form_validation->run()==TRUE){
                     if(!$this->SB_admin_m->ident_exist($this->input->post('ident')) ){
                    $data = array(
                    'ident'=>$this->input->post('ident'),
                    'pass'=>md5($this->input->post('pass')), 
                    'nom'=>$this->input->post('nom'),
                    'prenom'=>$this->input->post('prenom') 
                    );
                    $this->SB_admin_m->add_admin($data);
                    redirect('SB_admin/list_admin');
                }else { $this->_data['erreur'] = "Identifiant existe déja !";
                       $this->display_view('admin/add_admin');
                        }
                } else{
                    $this->_data['page_title'] = "Add Admin";
                    $this->display_view('admin/add_admin');    
                }
            } else{
                $this->_data['page_title'] = "Add Admin";
                $this->display_view('admin/add_admin');
            }
        }
        function change_pass(){
            is_admin();
            $id=$this->session->userdata('adminid');
            $this->form_validation->set_rules('old_pass', 'old_password','required');
            $this->form_validation->set_rules('new_pass', 'Password', 'required|matches[conf_pass]');
            $this->form_validation->set_rules('conf_pass', 'confirmation_password', 'required');
            if($this->input->post('submit')){
                //validate form va thay pass
                if ($this->form_validation->run()==TRUE){
                    $old_pass= md5($this->input->post('old_pass'));
                    //print_r($this->Admin_m->is_password_match($id,$old_pass));
                    if ($this->SB_admin_m->is_password_match($id,$old_pass)==TRUE) {
                        $new_pass=md5($this->input->post('new_pass'));
                        //thuc hien lenh doi password
                        $this->SB_admin_m->change_pass($id,$new_pass);
                        $this->_data['info'] = 'Votre mot de pass a été changé';
                        $this->_data['page_title'] = "Changer mot de pass";
                        $this->display_view('admin/change_pass');
                    }
                    else {
                        $this->_data['info'] = 'Votre ancien mot de passe ne correspondent pas';
                        $$this->_data['page_title'] = "Changer mot de pass";
                        $this->display_view('admin/change_pass');
                    }
                }
                else {
                    $this->_data['page_title'] = "Changer mot de pass";
                    $this->display_view('admin/change_pass');
                }
            }
            else {
                $this->_data['page_title'] = "Changer mot de pass";
                $this->display_view('admin/change_pass');}
        }  
        
        function delete_admin($id){
            is_admin();
            $this->SB_admin_m->delete_admin($id);
            redirect('SB_admin/list_admin');  
        }
        function log_out(){
            $this->session->sess_destroy();
            redirect('SB_admin/login');
        }
 //GESTION CONTENU 
 
    function gestion_contenu(){
        $this->accueil_contenu();
    }
    function accueil_contenu(){
        is_admin();
        $per_page = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($per_page == "") {
        $per_page = 10;
        }
        if ($offset == "") {
             $offset = 0;
             }
             $page="Accueil";
             $config['base_url'] = base_url() . 'index.php/SB_admin/accueil_contenu/' . $per_page . '/';
             $config['per_page'] = '10';
             $config['uri_segment'] = 4;
             $config['total_rows'] = $this->db->count_all('contenu');
             $this->_data['query'] = $this->SB_admin_m->get_contenu($page,$config['per_page'], $offset);
             $this->pagination->initialize($config);
             $this->_data['pagination'] = $this->pagination->create_links();
             $this->_data['page']=$page;
             $this->_data['page_title'] = "Gestion du contenu";
             $this->admin_view('admin/info');
        }
        function modifier_contenu($id){
            is_admin();
            $config = array(
            array(
            'field'   => 'titre',
            'label'   => 'Titre',
            'rules'   => 'required|xss_clean'
            ),
            array(
            'field'   => 'txtDes',
            'label'   => 'Description',
            'rules'   => 'required|xss_clean'
            ),
            );
            $this->_data['query']=$this->SB_admin_m->get_detail_contenu($id);
            $this->form_validation->set_rules($config); 
            if($this->input->post('submit')){
                if($this->form_validation->run()==TRUE){
                    $data = array(
                    'titre'=>$this->input->post('titre'),
                    'description'=>$this->input->post('txtDes') 
                    );
                    $this->SB_admin_m->modifier_contenu($data,$id);
                    redirect('SB_admin/gestion_contenu');   
                } else{
                    $this->_data['page_title'] = "Modifier contenu";
                    $this->display_view('admin/modifier_contenu');    
                }
            } else{
                $this->_data['page_title'] = "Modifier contenu";
                $this->display_view('admin/modifier_contenu');
            }      
        }    
    function add_contenu(){
            is_admin();
            $config = array(
            array(
            'field'   => 'titre',
            'label'   => 'Titre',
            'rules'   => 'required|xss_clean'
            ),
            array(
            'field'   => 'page',
            'label'   => 'Page',
            'rules'   => 'required|xss_clean'
            ),
            array(
            'field'   => 'txtDes',
            'label'   => 'Description',
            'rules'   => 'required|xss_clean'
            ),
         );
            $this->form_validation->set_rules($config); 
            if($this->input->post('submit')){
                if($this->form_validation->run()==TRUE){
                    $data = array(
                    'titre'=>$this->input->post('titre'),
                    'page'=>$this->input->post('page'),
                    'description'=>$this->input->post('txtDes') 
                    );
                    $this->SB_admin_m->add_contenu($data);
                    $per_page = $this->uri->segment(3);
                    $offset = $this->uri->segment(4);
                    if ($per_page == "") {
                    $per_page = 10;
                     }
                     if ($offset == "") {
                     $offset = 0;
                     }
                     $page=$this->input->post('page');
                     $this->_data['page']=$page;
                     $config['base_url'] = base_url() . 'index.php/SB_admin/accueil_contenu/' . $per_page . '/';
                      $config['per_page'] = '10';
                      $config['uri_segment'] = 4;
                      $config['total_rows'] = $this->db->count_all('contenu');
                      $this->_data['query'] = $this->SB_admin_m->get_contenu($page,$config['per_page'], $offset);
                      $this->pagination->initialize($config);
                      $this->_data['pagination'] = $this->pagination->create_links();
                      $this->_data['page_title'] = "Gestion du contenu";
                      $this->admin_view('admin/info');
                    
  
                } else{
                    $this->_data['page_title'] = "Add contenu";
                    $this->display_view('admin/accueil_contenu');    
                }
            } else{
                $this->_data['page_title'] = "Add contenu";
                $this->display_view('admin/accueil_contenu');
            }
            
        }
    
    function p_a_setb(){
        is_admin();
        $per_page = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($per_page == "") {
        $per_page = 10;
        }
        if ($offset == "") {
             $offset = 0;
             }
             $page="Pourquoi allier sport et business";
             $config['base_url'] = base_url() . 'index.php/SB_admin/p_a_setb' . $per_page . '/';
             $config['per_page'] = '10';
             $config['uri_segment'] = 4;
             $config['total_rows'] = $this->db->count_all('contenu');
             $this->_data['query'] = $this->SB_admin_m->get_contenu($page,$config['per_page'], $offset);
             $this->pagination->initialize($config);
             $this->_data['pagination'] = $this->pagination->create_links();
             $this->_data['page_title'] = "Gestion du contenu";
             $this->_data['page']=$page;
             $this->admin_view('admin/info');
       }
       
       function principe(){
        is_admin();
        $per_page = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($per_page == "") {
        $per_page = 10;
        }
        if ($offset == "") {
             $offset = 0;
             }
             $page="Principe";
             $config['base_url'] = base_url() . 'index.php/SB_admin/principe' . $per_page . '/';
             $config['per_page'] = '10';
             $config['uri_segment'] = 4;
             $config['total_rows'] = $this->db->count_all('contenu');
             $this->_data['query'] = $this->SB_admin_m->get_contenu($page,$config['per_page'], $offset);
             $this->pagination->initialize($config);
             $this->_data['pagination'] = $this->pagination->create_links();
             $this->_data['page_title'] = "Gestion du contenu";
             $this->_data['page']=$page;
             $this->admin_view('admin/info');
       }
       
       function objectif(){
        is_admin();
        $per_page = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($per_page == "") {
        $per_page = 10;
        }
        if ($offset == "") {
             $offset = 0;
             }
             $page="Les objectifs";
             $config['base_url'] = base_url() . 'index.php/SB_admin/objectif' . $per_page . '/';
             $config['per_page'] = '10';
             $config['uri_segment'] = 4;
             $config['total_rows'] = $this->db->count_all('contenu');
             $this->_data['query'] = $this->SB_admin_m->get_contenu($page,$config['per_page'], $offset);
             $this->pagination->initialize($config);
             $this->_data['pagination'] = $this->pagination->create_links();
             $this->_data['page_title'] = "Gestion du contenu";
             $this->_data['page']=$page;
             $this->admin_view('admin/info');
       }
       
       function equipe(){
        is_admin();
        $per_page = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($per_page == "") {
        $per_page = 10;
        }
        if ($offset == "") {
             $offset = 0;
             }
             $page="L'equipe";
             $config['base_url'] = base_url() . 'index.php/SB_admin/equipe' . $per_page . '/';
             $config['per_page'] = '10';
             $config['uri_segment'] = 4;
             $config['total_rows'] = $this->db->count_all('contenu');
             $this->_data['query'] = $this->SB_admin_m->get_contenu($page,$config['per_page'], $offset);
             $this->pagination->initialize($config);
             $this->_data['pagination'] = $this->pagination->create_links();
             $this->_data['page_title'] = "Gestion du contenu";
             $this->_data['page']=$page;
             $this->admin_view('admin/info');
       }
    function pre_adherence(){
        is_admin();
        $per_page = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($per_page == "") {
        $per_page = 10;
        }
        if ($offset == "") {
             $offset = 0;
             }
             $page="Presentation Adherence";
             $config['base_url'] = base_url() . 'index.php/SB_admin/pre_adherence' . $per_page . '/';
             $config['per_page'] = '10';
             $config['uri_segment'] = 4;
             $config['total_rows'] = $this->db->count_all('contenu');
             $this->_data['query'] = $this->SB_admin_m->get_contenu($page,$config['per_page'], $offset);
             $this->pagination->initialize($config);
             $this->_data['pagination'] = $this->pagination->create_links();
             $this->_data['page_title'] = "Gestion du contenu";
             $this->_data['page']=$page;
             $this->admin_view('admin/info');
       }
    function fonctionement(){
        is_admin();
        $per_page = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($per_page == "") {
        $per_page = 10;
        }
        if ($offset == "") {
             $offset = 0;
             }
             $page="Fonctionnement";
             $config['base_url'] = base_url() . 'index.php/SB_admin/fonctionement' . $per_page . '/';
             $config['per_page'] = '10';
             $config['uri_segment'] = 4;
             $config['total_rows'] = $this->db->count_all('contenu');
             $this->_data['query'] = $this->SB_admin_m->get_contenu($page,$config['per_page'], $offset);
             $this->pagination->initialize($config);
             $this->_data['pagination'] = $this->pagination->create_links();
             $this->_data['page_title'] = "Gestion du contenu";
             $this->_data['page']=$page;
             $this->admin_view('admin/info');
       }
    function pourquoi_adherer(){
        is_admin();
        $per_page = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($per_page == "") {
        $per_page = 10;
        }
        if ($offset == "") {
             $offset = 0;
             }
             $page="Pourquoi adherer?";
             $config['base_url'] = base_url() . 'index.php/SB_admin/pourquoi_adherer' . $per_page . '/';
             $config['per_page'] = '10';
             $config['uri_segment'] = 4;
             $config['total_rows'] = $this->db->count_all('contenu');
             $this->_data['query'] = $this->SB_admin_m->get_contenu($page,$config['per_page'], $offset);
             $this->pagination->initialize($config);
             $this->_data['pagination'] = $this->pagination->create_links();
             $this->_data['page_title'] = "Gestion du contenu";
             $this->_data['page']=$page;
             $this->admin_view('admin/info');
       }
    function regles(){
        is_admin();
        $per_page = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($per_page == "") {
        $per_page = 10;
        }
        if ($offset == "") {
             $offset = 0;
             }
             $page="Les regles";
             $config['base_url'] = base_url() . 'index.php/SB_admin/regles' . $per_page . '/';
             $config['per_page'] = '10';
             $config['uri_segment'] = 4;
             $config['total_rows'] = $this->db->count_all('contenu');
             $this->_data['query'] = $this->SB_admin_m->get_contenu($page,$config['per_page'], $offset);
             $this->pagination->initialize($config);
             $this->_data['pagination'] = $this->pagination->create_links();
             $this->_data['page_title'] = "Gestion du contenu";
             $this->_data['page']=$page;
             $this->admin_view('admin/info');
       }
    function mention_legale(){
        is_admin();
        $per_page = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($per_page == "") {
        $per_page = 10;
        }
        if ($offset == "") {
             $offset = 0;
             }
             $page="Mention legale";
             $config['base_url'] = base_url() . 'index.php/SB_admin/mention_legale' . $per_page . '/';
             $config['per_page'] = '10';
             $config['uri_segment'] = 4;
             $config['total_rows'] = $this->db->count_all('contenu');
             $this->_data['query'] = $this->SB_admin_m->get_contenu($page,$config['per_page'], $offset);
             $this->pagination->initialize($config);
             $this->_data['pagination'] = $this->pagination->create_links();
             $this->_data['page_title'] = "Gestion du contenu";
             $this->_data['page']=$page;
             $this->admin_view('admin/info');
       }
    function condition_adhesion(){
        is_admin();
        $per_page = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($per_page == "") {
        $per_page = 10;
        }
        if ($offset == "") {
             $offset = 0;
             }
             $page="La condition d'adhesion";
             $config['base_url'] = base_url() . 'index.php/SB_admin/condition_adhesion' . $per_page . '/';
             $config['per_page'] = '10';
             $config['uri_segment'] = 4;
             $config['total_rows'] = $this->db->count_all('contenu');
             $this->_data['query'] = $this->SB_admin_m->get_contenu($page,$config['per_page'], $offset);
             $this->pagination->initialize($config);
             $this->_data['pagination'] = $this->pagination->create_links();
             $this->_data['page_title'] = "Gestion du contenu";
             $this->_data['page']=$page;
             $this->admin_view('admin/info');
       }
    function inscription(){
        is_admin();
        $per_page = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($per_page == "") {
        $per_page = 10;
        }
        if ($offset == "") {
             $offset = 0;
             }
             $page="L'inscription";
             $config['base_url'] = base_url() . 'index.php/SB_admin/inscription' . $per_page . '/';
             $config['per_page'] = '10';
             $config['uri_segment'] = 4;
             $config['total_rows'] = $this->db->count_all('contenu');
             $this->_data['query'] = $this->SB_admin_m->get_contenu($page,$config['per_page'], $offset);
             $this->pagination->initialize($config);
             $this->_data['pagination'] = $this->pagination->create_links();
             $this->_data['page_title'] = "Gestion du contenu";
             $this->_data['page']=$page;
             $this->admin_view('admin/info');
       }
     function adherence(){
        is_admin();
        $per_page = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($per_page == "") {
        $per_page = 10;
        }
        if ($offset == "") {
             $offset = 0;
             }
             $page="L'adherence";
             $config['base_url'] = base_url() . 'index.php/SB_admin/adherence' . $per_page . '/';
             $config['per_page'] = '10';
             $config['uri_segment'] = 4;
             $config['total_rows'] = $this->db->count_all('contenu');
             $this->_data['query'] = $this->SB_admin_m->get_contenu($page,$config['per_page'], $offset);
             $this->pagination->initialize($config);
             $this->_data['pagination'] = $this->pagination->create_links();
             $this->_data['page_title'] = "Gestion du contenu";
             $this->_data['page']=$page;
             $this->admin_view('admin/info');
       }
    function newletters(){
        is_admin();
        $per_page = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($per_page == "") {
        $per_page = 10;
        }
        if ($offset == "") {
             $offset = 0;
             }
             $page="Newletters";
             $config['base_url'] = base_url() . 'index.php/SB_admin/newletters' . $per_page . '/';
             $config['per_page'] = '10';
             $config['uri_segment'] = 4;
             $config['total_rows'] = $this->db->count_all('contenu');
             $this->_data['query'] = $this->SB_admin_m->get_contenu($page,$config['per_page'], $offset);
             $this->pagination->initialize($config);
             $this->_data['pagination'] = $this->pagination->create_links();
             $this->_data['page_title'] = "Gestion du contenu";
             $this->_data['page']=$page;
             $this->admin_view('admin/info');
       }

             


    
 //GESTION IMAGE
    function gestion_image(){
        is_admin();
        $per_page = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($per_page == "") {
            $per_page = 10;
        }
        if ($offset == "") {
            $offset = 0;
        }
        $config['base_url'] = base_url() . 'index.php/SB_admin/gestion_image/' . $per_page . '/';
        $config['per_page'] = '10';
        $config['uri_segment'] = 4;
        $config['total_rows'] = $this->db->count_all('image');
        $this->_data['query'] = $this->SB_admin_m->get_image($config['per_page'], $offset);
        $this->pagination->initialize($config);
        //$this->_data['side_bar']="gestion_image";
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $this->db->count_all('image');
        $this->_data['page_title'] = "Gestion image";
        $this->display_view('admin/gestion_image');    
    }
    function add_image(){
        $image_path = './assets/photo';
        $thumb_path = './assets/thumb';
        $config = array(
            'allowed_types' => "jpg|jpeg|gif|png",
            'upload_path' => $image_path,
            'max_size'=>50000000
        );
        $this->upload->initialize($config);
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload())
            {
                $this->_data['error'] = $this->upload->display_errors();
                $eror=$this->upload->display_errors();
                echo $eror;
                die;
                $this->gestion_image();
            }    
        else
            {
                $upload_info = $this->upload->data();
                $config = array(
                'source_image' => $upload_info['full_path'], //get original image
                'new_image' => $thumb_path, //save as new image //need to create thumbs first
                'maintain_ratio' => true,
                'width' => 300,
                'height' => 200
                );
                $this->load->library('image_lib', $config); //load library
                $this->image_lib->resize(); //do whatever specified in config 
                $data = array(
                'name'=>$this->input->post('titre'),
                'page'=>$this->input->post('page'),
                'img'=>$upload_info['file_name'],
                );
                $this->SB_admin_m->add_image($data);
                $this->gestion_image();
                //print_r($data);
 
            }    
    }
    function update_image($id){
        $image_path = './assets/photo';
        $thumb_path = './assets/thumb';
        $config = array(
            'allowed_types' => "jpg|jpeg|gif|png",
            'upload_path' => $image_path,
            'max_size'=>50000000
        );
        $this->upload->initialize($config);
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload())
            {
                $this->_data['error'] = $this->upload->display_errors();
                $eror=$this->upload->display_errors();
                echo $eror;
                die;
                $this->gestion_image();
            }    
        else
            {
                $upload_info = $this->upload->data();
                $config = array(
                'source_image' => $upload_info['full_path'], //get original image
                'new_image' => $thumb_path, //save as new image //need to create thumbs first
                'maintain_ratio' => true,
                'width' => 300,
                'height' => 200
                );
                $this->load->library('image_lib', $config); //load library
                $this->image_lib->resize(); //do whatever specified in config 
                $data = array(
                'img'=>$upload_info['file_name'],
                'name'=>$this->input->post('titre')
                );
                $this->SB_admin_m->update_image($id,$data);
                $this->gestion_image();
                //print_r($data);
            }
        
    }
    function modifier_image($id){
        
        $this->_data['query']=$this->SB_admin_m->get_image_by_ID($id);
        
        $this->_data['page_title']="Modifier image";
        $this->display_view('admin/modifier_image');
    
    }


 //BEGIN LIST CONTACT
 

    
 //END CONTACT
 }
 ?>

 